package cn.explink.eap.web.system.service.impl;

import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.explink.eap.core.common.service.impl.CommonServiceImpl;
import cn.explink.eap.web.system.pojo.base.TSRoleUser;
import cn.explink.eap.web.system.pojo.base.TSUser;
import cn.explink.eap.web.system.service.UserService;

/**
 *
 * @author 张代浩
 *
 */
@Service("userService")
@Transactional
public class UserServiceImpl extends CommonServiceImpl implements UserService {

	@Override
	public TSUser checkUserExits(TSUser user) {
		return this.commonDao.getUserByUserIdAndUserNameExits(user);
	}

	@Override
	public String getUserRole(TSUser user) {
		return this.commonDao.getUserRole(user);
	}

	@Override
	public void pwdInit(TSUser user, String newPwd) {
		this.commonDao.pwdInit(user, newPwd);
	}

	@Override
	public int getUsersOfThisRole(String id) {
		Criteria criteria = this.getSession().createCriteria(TSRoleUser.class);
		criteria.add(Restrictions.eq("TSRole.id", id));
		int allCounts = ((Long) criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();
		return allCounts;
	}

	/**
	 *
	 * @Title: updateToken
	 * @Description: 更新token标识
	 * @author 孟凡岭
	 * @param @param id
	 * @param @param token
	 * @throws
	 */
	@Override
	public void updateToken(String id, String token) {
		// TODO Auto-generated method stub
		String sql = "update t_s_user set token='" + token + "' where id='" + id + "'";
		this.commonDao.updateBySqlString(sql);
	}

}
